package servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import svc.FileSvc;
import svc.LoginSvc;
import constants.Constants;
import domain.file.FileDetails;

/**
 * Servlet implementation class Login
 */
//@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private LoginSvc svc = new LoginSvc();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		HttpSession session = request.getSession(false);
		if (session == null) {
			session = request.getSession(true);		// New Session Created
		}

		String email = request.getParameter("email");
		String password = request.getParameter("password");

		request.setAttribute("userName", email);
		request.setAttribute("passWord", password);

		int userId = svc.isUidPwdValid(email, password);
		String userName = svc.getName(userId);
		
		FileSvc fileSvc = new FileSvc();
		ArrayList<FileDetails> userFileList = fileSvc.getUserFileList(userId);
		ArrayList<FileDetails> allFileList = fileSvc.getAllFileList(userId);
		ArrayList<FileDetails> analyzedFileList = fileSvc.getAnalyzedFileList(userId);
		
		if (null != email && null != password && userId > 0) {
			request.setAttribute("loginStatus", "Success");
			//request.setAttribute("loginMessage", "");
			session.setAttribute(Constants.SESSION_USER_ID, userId);
			session.setAttribute(Constants.SESSION_EMAIL_ID, email);
			session.setAttribute(Constants.SESSION_USER_NAME, userName);
			session.setAttribute(Constants.SESSION_ALL_SCENARIO_LIST, allFileList);
			session.setAttribute(Constants.SESSION_USER_SCENARIO_LIST, userFileList);
			session.setAttribute(Constants.SESSION_ANALYZED_SCENARIO_LIST, analyzedFileList);
			request.getRequestDispatcher("/jsp/dataset.jsp").forward(request, response);
		} else {
			String loginMessage = "Login Failed! Please try again.";
			request.setAttribute("loginMessage", loginMessage);
			request.setAttribute("loginStatus", "Fail");
			session.setAttribute(Constants.SESSION_USER_ID, -1);
			session.setAttribute(Constants.SESSION_EMAIL_ID, email);
			session.setAttribute(Constants.SESSION_USER_NAME, "");
			request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}

}
